package cn.dt.org.controller;

import cn.dt.basic.exception.BusinessException;
import cn.dt.basic.util.ExcelUtil;
import cn.dt.org.domain.ShopAuditLog;
import cn.dt.org.mapper.ShopMapper;
import cn.dt.org.service.IShopService;
import cn.dt.org.domain.Shop;
import cn.dt.org.query.ShopQuery;
import cn.dt.basic.util.AjaxResult;
import cn.dt.basic.util.PageList;
import cn.dt.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;

    /**
     * 导入数据
     * @param file
     */
    @PostMapping("/importExcel")
    @ResponseBody
    public void importExcel(@RequestPart("file") MultipartFile file){
        //String file = "F:\\文件名.xls";
        //解析excel，
        List<Shop> shops = ExcelUtil.importExcel(file,0,1,Shop.class);
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        System.out.println("导入数据一共【"+shops.size()+"】行");
        //保存到数据库中[自己实现，提示：动态sql 批量插入]
        shopService.batchSave(shops);

    }
    /**
     * 导出excel
     * @param
     * @return
     */
    @GetMapping("/export")
    @ResponseBody
    public void export( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.queryAll();
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 审核通过
     * @param log
     * @return
     */
    @PostMapping("/audit/pass")
    @ResponseBody
    public AjaxResult pass(@RequestBody ShopAuditLog log){
        try {
            shopService.savePass(log);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存日志失败！"+e.getMessage());
        }
    }

    /**
     * 审核驳回
     * @param log
     * @return
     */
    @PostMapping("/audit/reject")
    @ResponseBody
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存日志失败！"+e.getMessage());
        }
    }

    /**
     * 激活功能实现
     *
     * @param
     * @return
     */
    @RequestMapping("/activation")
    public String activation(Long id){
        shopService.activation(id);
       return "redirect:http://localhost:8081/#/login";
    }




    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @ResponseBody
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @ResponseBody
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @ResponseBody
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryPage(query);
    }

    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    @ResponseBody
    public AjaxResult settlement(@RequestBody  Shop shop)
    {
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
     *获取报表echar
     */
    @GetMapping("/echarts")
    @ResponseBody
    public Map<String,Object> echartsData(){
        List<ShopVo> shopVos=shopService.countByState();
        //把其中的数据转换为list或者数组  前端用于图表的y轴数据
        List<String> state = shopVos.stream().map(ShopVo::getState).collect(Collectors.toList());
        List<Integer> countNum = shopVos.stream().map(ShopVo::getCountNum).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        map.put("states",state);
        map.put("count",countNum);
        return map;
    }


}
